package eazy.ListNode;
import utils.ListNode_singly_linked;
public class getIntersectionNode_160 {
    public static void main(String[] args) {
        getIntersectionNode_160 getIntersectionNode_160 = new getIntersectionNode_160();
        ListNode_singly_linked l3 = new ListNode_singly_linked(3);
        ListNode_singly_linked l4 = new ListNode_singly_linked(4);
        ListNode_singly_linked l5 = new ListNode_singly_linked(5);
        ListNode_singly_linked a = new ListNode_singly_linked(1);
        ListNode_singly_linked b= new ListNode_singly_linked(1);
        a.next=l4;
        l4.next=l5;
        b.next=l3;
        l3.next=l4;
        ListNode_singly_linked intersectionNode = getIntersectionNode_160.getIntersectionNode(a, b);
        System.out.println(intersectionNode.val);
    }
    public ListNode_singly_linked getIntersectionNode(ListNode_singly_linked headA,
                                                      ListNode_singly_linked headB){
        if (headA==null||headB==null){
            return  null;
        }
        ListNode_singly_linked pA = headA, pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;


    }
}
